package entreprise;

import java.util.ArrayList;

import database.Database;
import mapping.DBFood;

public class Food extends DBFood {
	private Database database;

	public Food() {
		super();
		database = new Database("restoreservation");
	}

	public Food(String id_food) {
		super(id_food);
	}

	public Food(String id_category, String name_food, String description,
			String country, String image, String price) {
		super(id_category, name_food, description, country, image, price);
	}

	public Food(String id_food, String id_category, String name_food,
			String description, String country, String image, String price) {
		super(id_food, id_category, name_food, description, country, image,
				price);
	}

	public Food(Database database) {
		super();
		database = new Database("restoreservation");
		this.database = database;
	}

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public boolean addFood() {
		String sql_str = "";
		try {
			sql_str = "INSERT INTO food(id_category, name_food, description, country, image, price) VALUES("
					+ Integer.parseInt(this.getId_category())
					+ ", '"
					+ this.getName_food()
					+ "', '"
					+ this.getDescription()
					+ "', '"
					+ this.getcountry()
					+ "', '"
					+ this.getImage()
					+ "', " + Double.parseDouble(this.getPrice()) + ")";
			database.set(sql_str);

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean updateFood() {
		String sql_str = "";
		try {
			sql_str = "UPDATE food set id_category = "
					+ Integer.parseInt(this.getId_category())
					+ ", name_food = '" + this.getName_food()
					+ "', description = '" + this.getDescription()
					+ "', country = '" + this.getcountry() + "', image = '"
					+ this.getImage() + "', price = " + this.getPrice()
					+ " WHERE id_food = " + Integer.parseInt(this.getId_food());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean deleteFood() {
		String sql_str = "";
		try {
			sql_str = "DELETE FROM food WHERE id_food = "
					+ Integer.parseInt(this.getId_food());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public ArrayList<ArrayList<String>> listAllFood() {
		String str_sql = "select * from food";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<String> listFoodWithID(String id_food) {
		String str_sql = "select * from food where id_food = "
				+ Integer.parseInt(id_food);
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		ArrayList<String> data = new ArrayList<String>();
		try {
			datas = database.get(str_sql);
			if (datas.size() > 0)
				data = datas.get(0);
		} catch (Exception ex) {
		}
		return data;
	}
	
	public ArrayList<ArrayList<String>> listFoodWithCategory(String id_category) {
		String str_sql = "select * from food where id_category = "
				+ Integer.parseInt(id_category);
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		try {
			datas = database.get(str_sql);
			return datas;
		} catch (Exception ex) {
			return null;
		}
		
	}
}
